package com.easyj.admin.configuration;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.easyj.base.constant.ConstantProperties;
import com.easyj.base.constant.Constants;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;


/**
 * mvc 配置
 * @author hjf
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
	
	
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 本地文件上传路径 */
        registry.addResourceHandler(Constants.URSER_RESOURCE_PREFIX + "/**").addResourceLocations("file:" + ConstantProperties.LOCAL_PATH);

        /** swagger配置 */
        //registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        //registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        
        //static
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/").setCachePeriod(0);
    }
	
	
	/**改为在MappingJackson2HttpMessageConverterConfig定义，删除此方法
	 * 
	 * 
	     * 自定义字符串处理-：
	     * 空字符串转为 null 并对字符串 trim()，这样方便mybatis查询（用if null）
	    * 一般 的字符串Field 都可以这样处理，
	      * 有特别的，可加                         
	*   @JsonDeserialize(using = StringDeserializer.class)
	       *  使标准处理
	       
	  注意：converters 是一个list 也就是可有多个,spring mvc 应是取前面一个处理，事实上到这步，debbug 发现已生成两个MappingJackson2HttpMessageConverter
	  
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		ObjectMapper objectMapper = new ObjectMapper();
        // 对于空的对象转json的时候不抛出错误
		//objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        // 禁用遇到未知属性抛出异常
       // baseMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		//objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        // 低层级配置
		//objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS,true);
		//objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,true);
		//objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,true);
		//objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        
        //自定义字符串处理-----
       SimpleModule module = new SimpleModule();
       module.addDeserializer(String.class, new StringBlanktoNullDeserializer());
       objectMapper.registerModule(module);
        
	   converters.add(0,new MappingJackson2HttpMessageConverter(objectMapper));//放在前面
        
	}
   */
}
